package com.graduate.cec.initException;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.graduate.cec.common.JsonResult;
import com.graduate.cec.common.ResponseCode;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 登录密码或用户名错误
 */
public class SimpleAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(JsonResult.failed(ResponseCode.ERR_PASSWORD_OR_USERNAME,e.getMessage().equals("用户名或密码错误") ? "密码错误" : e.getMessage() )));
        response.flushBuffer();

    }


}
